package com.es.servlet; /**
 * 描述:
 * es-servlet-
 *
 * @author Derek
 * 创建时间：2022-04-22 9:25
 */

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/TestSession")
public class TestSession extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端的数据通过 request
        //获取session对象
        HttpSession session = request.getSession();
       // session.setMaxInactiveInterval(20);

        //session的状态
        boolean isNew = session.isNew();
        System.out.println(isNew);
        //
        String sessionId = session.getId();
        System.out.println(sessionId);

        //往客户端写数据 用 response
        response.setContentType("text/html;charset=utf-8");
        //注销会话
       // session.invalidate();
        PrintWriter out = response.getWriter();
        out.println("sessionID:"+sessionId);
        out.println("<BR>isNew:"+isNew);
        out.println(response.encodeRedirectURL("TestSession"));
        out.flush();

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
